<!DOCTYPE html>
<html>
<head>
    <title>客户</title>
    <style type="text/css">
        #connect-container {
            float: left;
            width: 400px
        }

        #connect-container div {
            padding: 5px;
        }

        #console-container {
            float: left;
            margin-left: 15px;
            width: 400px;
        }

        #console {
            border: 1px solid #CCCCCC;
            border-right-color: #999999;
            border-bottom-color: #999999;
            height: 170px;
            overflow-y: scroll;
            padding: 5px;
            width: 100%;
        }

        #console p {
            padding: 0;
            margin: 0;
        }
    </style>

    <script type="text/javascript">
        var ws = null;
        var url = null;
        var transports = [];

        var Chats = {
       		getCustomerMessageInstance : function(type, subType, content){
       			var message = {};
       			message.type=type;
       			message.subType=subType;
       			message.content=content;
       			return message;
       		}
        };
        
        function setConnected(connected) {
            document.getElementById('connect').disabled = connected;
            document.getElementById('disconnect').disabled = !connected;
            document.getElementById('echo').disabled = !connected;
        }

        function connect() {
        	updateUrl();
        	console.log("url : "+url);
            if (!url) {
                alert('Select whether to use W3C WebSocket or SockJS');
                return;
            }
			
            ws = new WebSocket(url);
            
            ws.onopen = function () {
                setConnected(true);
                log('Info: connection opened.');
            };
            ws.onmessage = function (event) {
                log('Received: ' + event.data);
            };
            ws.onclose = function (event) {
            	debugger;
                setConnected(false);
                log('Info: connection closed.');
                log(event);
            };
        }

        function disconnect() {
            if (ws != null) {
                ws.close();
                ws = null;
            }
            setConnected(false);
        }

        function echo() {
            if (ws != null) {
                var message = document.getElementById('message').value;
                log('Sent: ' + message);
                ws.send(JSON.stringify(Chats.getCustomerMessageInstance(0, 0, message)));
            } else {
                alert('connection not established, please connect.');
            }
        }

        function updateUrl() {
        	urlPath = "/scrapMetal/chatConnect.do?chatInitiator=C&openId=o06M7wLa0Ptdy3voR3FgVLxTDaR4&source=2&sourceId=302";
            if (urlPath.indexOf('sockjs') != -1) {
                url = urlPath;
                document.getElementById('sockJsTransportSelect').style.visibility = 'visible';
            } else {
              if (window.location.protocol == 'http:') {
                  url = 'ws://' + window.location.host + urlPath;
              } else {
                  url = 'wss://' + window.location.host + urlPath;
              }
              document.getElementById('sockJsTransportSelect').style.visibility = 'hidden';
            }
        }

        function updateTransport(transport) {
        	alert(transport);
          transports = (transport == 'all') ?  [] : [transport];
        }
        
        function log(message) {
            var console = document.getElementById('console');
            var p = document.createElement('p');
            p.style.wordWrap = 'break-word';
            p.appendChild(document.createTextNode(message));
            console.appendChild(p);
            while (console.childNodes.length > 25) {
                console.removeChild(console.firstChild);
            }
            console.scrollTop = console.scrollHeight;
        }
        function pressure(){
        	var pressureTimesStr = document.getElementById("pressureTimes").value;
        	if(!pressureTimesStr){
        		alert("请填写压力测试次数");
        		return;
        	}
        	if(!url){
        		document.getElementById("radio1").click();
        	}
        	var pressureTimes = parseInt(pressureTimesStr);
        	
        	var myWs = new WebSocket(url);
    		myWs.onopen = function () {
                setConnected(true);
                log('Info: connection opened.');
                console.log("第"+ (successCount++) +"连接，创建成功");
                array.push(myWs);
            };
            myWs.onmessage = function (event) {
                log('Received: ' + event.data);
            };
            myWs.onclose = function (event) {
                setConnected(false);
                log('Info: connection closed.');
                log(event);
            };
            if(connectCount++ < pressureTimes && isPressure){
            	setTimeout("pressure()", 300);
            }
            
        }
        function startPressure(){
        	isPressure = true;
        }
        function stopPressure(){
        	isPressure = false;
        }
        var successCount = 0;
        var connectCount = 0;
        var array = [];
        var isPressure = true;
    </script>
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets 
    rely on Javascript being enabled. Please enable
    Javascript and reload this page!</h2></noscript>
<div>
    <div id="connect-container">
        <div id="sockJsTransportSelect" style="visibility:hidden;">
            <span>SockJS transport:</span>
            <select onchange="updateTransport(this.value)">
              <option value="all">all</option>
              <option value="websocket">websocket</option>
              <option value="xhr-polling">xhr-polling</option>
              <option value="jsonp-polling">jsonp-polling</option>
              <option value="xhr-streaming">xhr-streaming</option>
              <option value="iframe-eventsource">iframe-eventsource</option>
              <option value="iframe-htmlfile">iframe-htmlfile</option>
            </select>
        </div>
        <div>
            <button id="connect" onclick="connect();">Connect</button>
            <button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
        </div>
        <div>
            <textarea id="message" style="width: 350px">客户</textarea>
        </div>
        <div>
            <button id="echo" onclick="echo();" disabled="disabled">Echo message</button>
        </div>
    </div>
    <div id="console-container">
        <div id="console"></div>
    </div>
</div>
测试次数：<input type="text" id="pressureTimes" />
<input type="button" value="压力测试" onclick="pressure()"/>
<input type="button" value="开始测试" onclick="startPressure()"/>
<input type="button" value="停止测试" onclick="stopPressure()"/>
</body>
</html>